package com.youmingxing.activity.selfInfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.bean.selfinfo.AuditionsSuccessActivity;
import com.youmingxing.bean.selfinfo.DateInfo;
import com.youmingxing.bean.selfinfo.TimeInfo;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.utils.Tools;
import com.youmingxing.view.DateView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 选择置顶时间
 */
public class ChooseTimeActivity extends BaseActivity implements View.OnClickListener {

    private ImageView auditionsImageView; //作品图片
    private TextView tipsTextView;   // 置顶时间

    private LinearLayout ll_date;
    private GridView mGridView; // 显示时间段

    private TextView nextTextView; // 下一步
    private List<DateInfo> mDateInfos = new ArrayList<>(); //置顶日期
    private List<TimeInfo> mTimeInfos = new ArrayList<>(); //置顶时间

    private String cardno;
    private int update_id;
    private String item_id;
    private String tips;//置顶时间

    private String url;

    private DateView lastDateView = null; // 上一次选择的日期
    private DateView newDateView = null; // 本次选择的日期

    private ChooseTimeAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_choose_time);
        update_id = getIntent().getIntExtra("update_id", 0);
        cardno = getIntent().getStringExtra("cardno");
        url = getIntent().getStringExtra("url");
        tips = getIntent().getStringExtra("tips");
        initView();
        AllStarApi.getCouponsDate(cardno, dateHandler);
    }

    private void initView() {
        setTopbarName(R.string.time_choose_activity_my_work);
        auditionsImageView = (ImageView) findViewById(R.id.content_image);
        Glide.with(this).load(url).placeholder(R
                .drawable.default_16_9).animate(R.anim.abc_fade_in).into(auditionsImageView);

        tipsTextView = (TextView) findViewById(R.id.tv_time);
        tipsTextView.setText("置顶时间：" + tips);
        ll_date = (LinearLayout) findViewById(R.id.ll_date);
        mGridView = (GridView) findViewById(R.id.gv_time);
        nextTextView = (TextView) findViewById(R.id.tv_next);
        nextTextView.setOnClickListener(this);
    }

    BaseJsonHandler dateHandler = new BaseJsonHandler(this) {
        public void onSuccessWithData(JSONObject response) {
            JSONArray data = response.optJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                JSONObject jsonObject = data.optJSONObject(i);
                DateInfo dateInfo = new DateInfo();
                dateInfo.setItem_id(jsonObject.optString("item_id"));
                dateInfo.setTitle(jsonObject.optString("title"));
                dateInfo.setDate(jsonObject.optString("date"));
                mDateInfos.add(dateInfo);
            }
            initDate();
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        lastDateView = null;
        if (newDateView != null) {
            newDateView.view.setVisibility(View.INVISIBLE);
            newDateView = null;
            mTimeInfos.clear();
            mAdapter.notifyDataSetChanged();
        }
    }

    private void initDate() {
        for (final DateInfo mDateInfo : mDateInfos) {
            final DateView dateView = new DateView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                    (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            dateView.tv_title.setText(mDateInfo.getTitle());
            dateView.tv_date.setText(mDateInfo.getDate());
            dateView.setTag(mDateInfo.getItem_id());
            params.weight = 1;
            params.leftMargin = 15;
            dateView.setLayoutParams(params);
            dateView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (lastDateView == null) {
                        lastDateView = (DateView) v;
                        newDateView = (DateView) v;
                    } else {
                        lastDateView = newDateView;
                        newDateView = (DateView) v;
                    }
                    lastDateView.view.setVisibility(View.INVISIBLE);
                    newDateView.view.setVisibility(View.VISIBLE);
                    Toast.makeText(ChooseTimeActivity.this, newDateView.getTag().toString(), Toast.LENGTH_SHORT).show();
                    mTimeInfos.clear();
                    AllStarApi.getCouponsTime(cardno, newDateView.getTag().toString(), timeHandler);
                }
            });
            ll_date.addView(dateView);
        }
    }

    BaseJsonHandler timeHandler = new BaseJsonHandler(this) {
        public void onSuccessWithData(JSONObject response) {
            JSONArray data = response.optJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                JSONObject jsonObject = data.optJSONObject(i);
                TimeInfo timeInfo = new TimeInfo();
                timeInfo.setChoose_status(jsonObject.optInt("choose_status"));
                timeInfo.setTopset_id(jsonObject.optInt("topset_id"));
                timeInfo.setPeroid(jsonObject.optString("peroid"));
                mTimeInfos.add(timeInfo);
            }
            mAdapter = new ChooseTimeAdapter();
            mGridView.setAdapter(mAdapter);

        }
    };

    private class ChooseTimeAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mTimeInfos.size();
        }

        @Override
        public Object getItem(int position) {
            return mTimeInfos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(ChooseTimeActivity.this, R.layout.item_gv_time, null);
                holder.mImageView = (ImageView) convertView.findViewById(R.id.iv_bg);
                holder.mTextView = (TextView) convertView.findViewById(R.id.tv_time);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.mTextView.setText(mTimeInfos.get(position).getPeroid());
            GridView.LayoutParams params = new GridView.LayoutParams(Tools.dip2px(ChooseTimeActivity.
                    this,82), Tools.dip2px(ChooseTimeActivity.this,35));
            convertView.setLayoutParams(params);
            return convertView;
        }
    }

    private class ViewHolder {
        public ImageView mImageView;
        public TextView mTextView;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(ChooseTimeActivity.this, AuditionsSuccessActivity.class);
        startActivity(intent);
        Toast.makeText(this, (update_id + "  " + cardno), Toast.LENGTH_SHORT).show();
    }
}
